package krow.dev.contest;

import krow.dev.contest.Constants.Invoke;
import krow.dev.contest.R.drawable;
import krow.dev.contest.controller.DbOpenHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ContestDetailActivity extends Activity implements OnClickListener {

	private DbOpenHelper mDbOpenHelper;
	private Cursor mCursor;
	private Cursor mCursor1;

	private ScrollView mScrollView;

	private TextView mDescriptionView;

	private TextView mTitleView;

	private TextView mButtonText;

	private ImageView mWebButton;

	private View mFavoriteButton;

	String mTitle = null;
	String mCreator = null;
	String mDepartment = null;
	String mDescription = null;
	String mURL = null;
	String mCode = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		mDbOpenHelper = new DbOpenHelper(this);

		Intent intent = getIntent();
		if (intent != null) {
			mTitle = intent.getStringExtra(Invoke.KEY_TITLE);
			mCreator = intent.getStringExtra(Invoke.KEY_CREATOR);
			mDepartment = intent.getStringExtra(Invoke.KEY_DEPARTMENT);
			mDescription = intent.getStringExtra(Invoke.KEY_DESCRIPTION);
			mURL = intent.getStringExtra(Invoke.KEY_URL);
			int a = mURL.indexOf('=');
			mCode = mURL.substring(a + 1, mURL.length()); // mURL에서 맨뒤에 있는 숫자코드만
															// 빼냄

		}

		initilaizeView();

		applyDescription(mDescription);
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.layout_back:
			finish();
			break;
		case R.id.layout_favorite:
			mCursor = null;
			mDbOpenHelper.open();
			String x = null;
			mCursor = mDbOpenHelper.getMatchURL(mCode);
			if (mCursor.getCount() == 0) {
				System.out.println("db에서 해당코드가 존재하지않으니");
				Toast.makeText(ContestDetailActivity.this, "위시리스트에 등록되었습니다.", Toast.LENGTH_SHORT).show();
				mFavoriteButton.setBackgroundResource(R.drawable.ic_action_important);
				mDbOpenHelper.insertColumn(mTitle, mCreator, mDepartment,
						mDescription, mCode);
				mDbOpenHelper.close();
			} else {
				while (mCursor.moveToNext()) {
					x = mCursor.getString(mCursor.getColumnIndex("url"));
				}
				if (x.length() > 0) {
					System.out.println("db에서 해당코드가 존재함");
					Toast.makeText(ContestDetailActivity.this,"위시리스트에서 제거되었습니다.", Toast.LENGTH_SHORT).show();
					mFavoriteButton.setBackgroundResource(R.drawable.ic_action_not_important);
					mDbOpenHelper.deleteColumn(mCode);
					mDbOpenHelper.close();
				}
				mCursor.close();
			}

			break;

		/*
		 * boolean favoriteCheck = false;
		 * 
		 * if (favoriteCheck == false) {
		 * Toast.makeText(ContestDetailActivity.this, "위시리스트에 등록되었습니다.",
		 * Toast.LENGTH_SHORT).show(); favoriteCheck = true;
		 * 
		 * 
		 * mDbOpenHelper.open(); mDbOpenHelper.insertColumn(mTitle, mCreator,
		 * mDepartment, mDescription, mCode); mDbOpenHelper.close();
		 * 
		 * } else { Toast.makeText(ContestDetailActivity.this,
		 * "위시리스트에서 제거되었습니다.", Toast.LENGTH_SHORT).show(); favoriteCheck =
		 * false; mDbOpenHelper.open(); mDbOpenHelper.deleteColumn(mCode);
		 * mDbOpenHelper.close();
		 * mFavoriteButton.setBackground(drawable.ic_drawer); }
		 */

		}
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
	}

	private void initilaizeView() {

		mTitleView = (TextView) findViewById(R.id.text_title);
		mTitleView.setText(mTitle);
		mTitleView.setSelected(true);

		findViewById(R.id.layout_back).setOnClickListener(this);
		findViewById(R.id.layout_favorite).setOnClickListener(this);

		mDescriptionView = (TextView) findViewById(R.id.text_description);
		mDescriptionView.setText(mDescription);

		mButtonText = (TextView) findViewById(R.id.web_text);
		mButtonText.setVisibility(View.INVISIBLE);
		mWebButton = (ImageView) findViewById(R.id.web_btn);
		mWebButton.setVisibility(View.INVISIBLE);
		mWebButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Intent.ACTION_VIEW);
				Uri u = Uri
						.parse("http://m.campusmon.com/Contest_View_Detail_Expand.asp?sn="
								+ mCode);
				i.setData(u);
				startActivity(i);
			}
		});

		mScrollView = (ScrollView) findViewById(R.id.scrollview);
		mScrollView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					mButtonText.setVisibility(View.INVISIBLE);
					mWebButton.setVisibility(View.INVISIBLE);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					mButtonText.setVisibility(View.VISIBLE);
					mWebButton.setVisibility(View.VISIBLE);

				}
				return false;
			}
		});
		
		mCursor1 = null;
		mDbOpenHelper.open();
		String x1 = null;
		mCursor1 = mDbOpenHelper.getMatchURL(mCode);
		
		mFavoriteButton = (View) findViewById(R.id.favorite_btn);
		
		if (mCursor1.getCount() == 0) {
			
			mFavoriteButton.setBackgroundResource(R.drawable.ic_action_not_important);
			mDbOpenHelper.close();
			
		} else {
			while (mCursor1.moveToNext()) {
				x1 = mCursor1.getString(mCursor1.getColumnIndex("url"));
			}
			if (x1.length() > 0) {
				mFavoriteButton.setBackgroundResource(R.drawable.ic_action_important);
				mDbOpenHelper.close();
			}
			mCursor1.close();
		}
		
	}

	private void applyDescription(String description) {

		StringBuilder stringBuilder = new StringBuilder();

		String[] stringArray = description.split("\\●");

		try {
			for (int i = 1; i < stringArray.length - 1; i++) {
				stringBuilder.append(Html.fromHtml(stringArray[i]).toString());
				stringBuilder.append('\n');
			}
		} catch (Exception e) {

		}

		mDescriptionView.setText(stringBuilder.toString());
	}
	/*
	 * // 참고용 private TranslateAnimation animation() { TranslateAnimation ani =
	 * new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f,
	 * Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
	 * Animation.RELATIVE_TO_SELF, 0.0f); ani.setFillAfter(true); // 애니메이션 후
	 * 이동한좌표에 //ani.setFillEnabled(true); ani.setDuration(500); // 지속시간
	 * 
	 * return ani; }
	 */

}
